<?php
function mail_attachment ($to, $subject, $txt_message, $attachment){
	$fileatt_type = "application/octet-stream"; // File Type 

	$from = "DRSA <info@DRSA.com>"; 
	
	$largo = strlen($attachment);
	if(substr($attachment,($largo-1),$largo) == ";"){
		$attachment = substr($attachment,0,($largo-1));
	}

	$files = explode(";",$attachment);


	$headers = "From: $from";

	for($i = 0; $i < count($files) ; $i++){
		$fileatt = $files[$i];
		$fileatttype = "application/pdf"; 
		$parts = explode("/",$fileatt);
		$fileattname = $parts[(count($parts)-1)];
		$file = fopen( $fileatt, 'rb' ); 
		$data = fread( $file, filesize( $fileatt ) ); 
		fclose( $file );
	
		$semi_rand = md5( time() ); 
		$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
	
		$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"\n";
	
		$data = chunk_split( base64_encode( $data ) );
		$message .= "--{$mime_boundary}\n" . 
					 "Content-Type: {$fileatttype};\n" . 
					 " name=\"{$fileattname}\"\n" . 
					 "Content-Disposition: attachment;\n" . 
					 " filename=\"{$fileattname}\"\n" . 
					 "Content-Transfer-Encoding: base64\n\n" . 
					 $data . "\n\n" ;

	}//for

	//$orig_message =str_replace("\n","<br>",$txt_message);
	$orig_message = $txt_message;
	
	$message .= "This is a multi-part message in MIME format.\n\n" . 
                "--{$mime_boundary}\n" . 
                "Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
               "Content-Transfer-Encoding: 7bit\n\n" . $orig_message . "\n\n"; 


	$headers .= "X-Sender: <info@DRSA.com>\n";
	$headers .= "X-Mailer: PHP\n"; //mailer
	$headers .= "X-Priority: 3\n"; //1 UrgentMessage, 3 Normal
	$headers .= "Return-Path: <info@DRSA.com>\n";		

	if( mail( $to, $subject, $message, $headers ) ) {
	 
	}
	else { 
		echo "<p>There was an error sending the mail.</p>"; 
	 
	}
}
?>
